@tool
class_name ContextBehavior_Face
extends ContextBehavior_MatchRotation

#region 属性
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init():
	super()
func calc_steering(behaviors_owner:Node,agent:SteeringBehaviorAgent,delta:float):
	super(behaviors_owner,agent,delta)
#endregion
#region 公共方法
func get_target_pos(target:Node2D) -> Vector2:
	return target.global_position
func calc_angular_interest():
	var detect_component:DetectComponent = agent.entity.get_component(DetectComponent)
	if not detect_component:
		return
	var detector_target:Detector_Target = detect_component.get_detector(GameEnum.DetectorType.Target)
	if not detector_target:
		return
	var targets = detector_target.get_targets(agent.get_pos(),0,Callable(),1)
	if targets.size() < 1:
		return
	var closed_target = targets[0]
	## 如果有逻辑Body，获取逻辑Body
	var target_entity:Entity = Utils.find_child_by_type(closed_target,Entity,2)
	if target_entity:
		closed_target = target_entity.logical_body
	var to_target:Vector2 = get_target_pos(closed_target) - agent.entity.logical_body.global_position
	var target_rot = to_target.normalized().angle()
	target_rot = wrapf(target_rot,-PI,PI)
	var angular_interest:float = get_match_rotation(target_rot) * sensitive
	agent.add_angular_forcce_interest(angular_interest * force)
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
	behavior_type = GameEnum.ContextBehaviorType.Face
func draw() -> void:
	super.draw()
#endregion
